package conf

import (
	"fmt"
	"WchimeGinSystem/utils/encryption"
	"log"

	"gopkg.in/ini.v1"
)

const SERVER_NAME = "gin_system"

var (
	SECRET       string
	MODE         string
	RUNPORT      string
	STORAGE_TYPE string

	MYSQL_USER     string
	MYSQL_PWD      string
	MYSQL_HOST     string
	MYSQL_PORT     string
	MYSQL_DATABASE string

	REDIS_HOST    string
	REDIS_PORT    string
	REDIS_PWD     string
	REDIS_LIBRARY int

	MINIO_HOST   string
	MINIO_PORT   string
	MINIO_USER   string
	MINIO_PWD    string
	MINIO_BUCKET string
	MINIO_SECURE bool

	COS_SECRET_ID  string
	COS_SECRET_KEY string
	COS_REGION     string
	COS_SCHEME     string
	COS_BUCKET     string
	COS_DOMAIN     string
)

func init() {
	conf, err := ini.Load("config.ini")
	if err != nil {
		fmt.Println("配置文件读取错误，请检查文件路径:", err)
		log.Fatalln("配置文件读取错误，请检查文件路径")
	}

	SECRET = conf.Section("serve").Key("secret").MustString("123456")
	MODE = conf.Section("serve").Key("mode").MustString("debug")
	RUNPORT = conf.Section("serve").Key("port").MustString("0.0.0.0:8080")
	STORAGE_TYPE = conf.Section("serve").Key("storage").MustString("minio")

	MYSQL_HOST = conf.Section("mysql").Key("host").MustString("127.0.0.1")
	MYSQL_PORT = conf.Section("mysql").Key("port").MustString("3306")
	MYSQL_USER = conf.Section("mysql").Key("user").MustString("")
	MYSQL_PWD = conf.Section("mysql").Key("password").MustString("")
	MYSQL_DATABASE = conf.Section("mysql").Key("database").MustString("")

	REDIS_HOST = conf.Section("redis").Key("host").MustString("127.0.0.1")
	REDIS_PORT = conf.Section("redis").Key("port").MustString("6379")
	REDIS_PWD = conf.Section("redis").Key("password").MustString("")
	REDIS_LIBRARY = conf.Section("redis").Key("library").MustInt(0)

	MINIO_HOST = conf.Section("minio").Key("host").MustString("127.0.0.1")
	MINIO_PORT = conf.Section("minio").Key("port").MustString("9000")
	MINIO_USER = conf.Section("minio").Key("user").MustString("")
	MINIO_PWD = conf.Section("minio").Key("password").MustString("")
	MINIO_BUCKET = conf.Section("minio").Key("bucket").MustString("")
	MINIO_SECURE = conf.Section("minio").Key("secure").MustBool(false)

	COS_SECRET_ID = conf.Section("cos").Key("secret_id").MustString("")
	COS_SECRET_KEY = conf.Section("cos").Key("secret_key").MustString("")
	COS_REGION = conf.Section("cos").Key("region").MustString("")
	COS_SCHEME = conf.Section("cos").Key("scheme").MustString("")
	COS_BUCKET = conf.Section("cos").Key("bucket").MustString("")
	COS_DOMAIN = conf.Section("cos").Key("domain").MustString("")

	MYSQL_USER = encryption.GetDataAes(SECRET, MYSQL_USER)
	REDIS_PWD = encryption.GetDataAes(SECRET, REDIS_PWD)
	MYSQL_PWD = encryption.GetDataAes(SECRET, MYSQL_PWD)
	// MINIO_USER = encryption.GetDataAes(SECRET, MINIO_USER)
	// MINIO_PWD = encryption.GetDataAes(SECRET, MINIO_PWD)

	if MYSQL_USER == "" || MYSQL_PWD == "" || REDIS_PWD == "" {
		log.Fatalln("配置文件解密错误，请检查文件")
	}

}
